package com.demo2do.fms.admin.entity.report.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class RepairFeeReportSearch implements QueryBuilder {
    
    /** 公司 */
    private List<Long> organizationIds;
    
    /** 所选的动力类型 */
    private Long powerTypeId;
    
    /** 公里数范围的上限 */
    private Integer rangeFrom;
    
    /** 公里数范围的下限 */
    private Integer rangeTo;
    
    /** 导出条数上限 */
    private Integer limit;
    
    private QueryOrder order;
    
    private QueryFilter filter;
    
    /**
     * The default constructor
     */
    public RepairFeeReportSearch(){
        order = new QueryOrder("a.organization_name", "ASC")
                          .add("f", "create_date", "ASC");
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (filter == null || refresh) {

            filter = new QueryFilter().addEquals("f", "power_type_id", powerTypeId)
                                      .addGreaterThanEqual("f", "range_from", rangeFrom)
                                      .addLessThanEqual("f", "range_to", rangeTo)
                                      .addSentence("f.organization_id IN (:organizationIds)", "organizationIds", organizationIds);
        }
        
        return filter;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {

        StringBuffer buffer = new StringBuffer(256);
        
        buffer.append("SELECT a.organization_name as organizationName, ")
                     .append("b.vehicle_feature_code as vehicleFeatureCode, ")
                     .append("b.vehicle_feature_name as vehicleFeatureName, ")
                     .append("f.range_from AS rangeFrom, ")
                     .append("f.range_to AS rangeTo, ")
                     .append("f.price as price ")
               .append("FROM tbl_truck_repair_fee f, vw_transportation_company a, vw_vehicle_feature b ")
               .append("WHERE f.organization_id = a.id and f.power_type_id = b.id AND f.is_disabled = 0 ")
               .append(getPreparedFilter(false).clauseSentence() + order.sentence());

        String sentence = "SELECT * FROM (" + buffer.toString() + ") WHERE ROWNUM <=" + limit;
        return sentence;

    }

    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {

        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {
            
            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("organizationName", rs.getObject("organizationName"));
                result.put("vehicleFeatureCode", rs.getObject("vehicleFeatureCode"));
                result.put("vehicleFeatureName", rs.getObject("vehicleFeatureName"));
                result.put("rangeFrom", rs.getObject("rangeFrom")); 
                result.put("rangeTo", rs.getObject("rangeTo")); 
                result.put("price", rs.getObject("price"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }
      
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
      
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }

    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the powerTypeId
     */
    public Long getPowerTypeId() {
        return powerTypeId;
    }

    /**
     * @return the rangeFrom
     */
    public Integer getRangeFrom() {
        return rangeFrom;
    }

    /**
     * @return the rangeTo
     */
    public Integer getRangeTo() {
        return rangeTo;
    }

    /**
     * @return the limit
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param powerTypeId the powerTypeId to set
     */
    public void setPowerTypeId(Long powerTypeId) {
        this.powerTypeId = powerTypeId;
    }

    /**
     * @param rangeFrom the rangeFrom to set
     */
    public void setRangeFrom(Integer rangeFrom) {
        this.rangeFrom = rangeFrom;
    }

    /**
     * @param rangeTo the rangeTo to set
     */
    public void setRangeTo(Integer rangeTo) {
        this.rangeTo = rangeTo;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

}
